
#include "stdafx.h"
#include "MinTimeDisplay.h"
#include "DPIForm.h"
#include "FontFactory.h"

IMPLEMENT_DYNAMIC(CMinTimeDisplay, CTimeDisplay)

CMinTimeDisplay::CMinTimeDisplay()
{
	m_fFactory = new CFontFactory();
	m_fFactory->Create(_T("Arial"), 38, 14, FW_BOLD)->SetMark(FM_TimeNum);
	HDC hScrDC = CreateScrDC();
	CSize szTime;
	HFONT oldFont = (HFONT)::SelectObject(hNumMemDC, m_fFactory->GetMark(FM_TimeNum));
	for (intptr_t i = 0; i < 11; i++)
	{
		if (i < 10) swprintf_s(nmTime[i].v, L"%d", (int)i); else wcscpy_s(nmTime[i].v, L":");
		::GetTextExtentPoint32(hNumMemDC, nmTime[i].v, 1, &nmTime[i].sz);
		if (i < 10) nmTime[i].getSize(i, szTime);
	}
	for (intptr_t i = 0; i < 11; i++)
	{
		if (i < 10)
		{
			nmTimePos[i] = CPoint((szTime.cx - nmTime[i].sz.cx) >> 1, (szTime.cy - nmTime[i].sz.cy) >> 1);
			CreateNum(hScrDC, &nmTime[i], szTime);
		}
		else
		{
			nmTimePos[i] = CPoint(0, 0);
			CreateNum(hScrDC, &nmTime[i], nmTime[i].sz);
		}
	}
	for (int i = 0, x = CDPIForm::DPIX(10), y = 0; i < 8; i++)		//00:00:00
	{
		numRect[i].left = x;
		if (i == 2 || i == 5)
		{
			x += nmTime[10].sz.cx; numIndex[i] = 10;
			numRect[i].top = y + ((szTime.cy - nmTime[10].sz.cy) >> 1);
			numRect[i].bottom = numRect[i].top + nmTime[10].sz.cy;
		}
		else
		{
			x += szTime.cx; numIndex[i] = -1;
			numRect[i].top = y;
			numRect[i].bottom = numRect[i].top + szTime.cy;
		}
		numRect[i].right = x;
	}
	CreateMemBitmap(hScrDC, numRect[7].right + CDPIForm::DPIX(10), numRect[7].bottom);
	numRectNA = CRect(0, 0, (int)width, (int)height); hBitmapNA = drawNA(::CreateCompatibleBitmap(hScrDC, (int)width, (int)height));
	::SelectObject(hNumMemDC, oldFont);
	::DeleteDC(hScrDC);
	time.Set(-1, -1, -1); clrText = RGB(0, 0, 0); isNA = false;
}

CMinTimeDisplay::~CMinTimeDisplay()
{
	for (intptr_t i = 0; i < 11; i++)
	{
		if (nmTime[i].bmp) ::DeleteObject(nmTime[i].bmp);
	}
	if (hBitmapNA) ::DeleteObject(hBitmapNA);
	delete m_fFactory;
}

BEGIN_MESSAGE_MAP(CMinTimeDisplay, CTimeDisplay)
END_MESSAGE_MAP()

void CMinTimeDisplay::setTextColor(COLORREF clr)
{
	if (clrText == clr) return;
	clrText = clr; ::SetTextColor(hNumMemDC, clrText);
	HFONT oldFont = (HFONT)::SelectObject(hNumMemDC, m_fFactory->GetMark(FM_TimeNum));
	for (intptr_t i = 0; i < 11; i++)
	{
		HBITMAP hold = (HBITMAP)::SelectObject(hNumMemDC, nmTime[i].bmp);
		::TextOut(hNumMemDC, nmTimePos[i].x, nmTimePos[i].y, nmTime[i].v, 1);
		::SelectObject(hNumMemDC, hold);
	}
	drawNA(hBitmapNA, clrText);
	::SelectObject(hNumMemDC, oldFont);
	if (isNA)
		CTimeDisplay::drawOut(numRectNA, hBitmapNA);
	else
		for (intptr_t i = 0; i < 8; i++) drawOut(i, numIndex[i]);
}

bool CMinTimeDisplay::setTime(int h, int m, int s)
{
	displayChanged = false;
	if (time.hour != h)
	{
		time.hour = h;
		intptr_t v = (intptr_t)(time.hour / 10) & 0xf; if (v != numIndex[0]) drawOut(0, v);
		v = time.hour % 10; if (v != numIndex[1]) drawOut(1, v);
	}
	if (time.minute != m)
	{
		time.minute = m;
		intptr_t v = (intptr_t)(time.minute / 10) & 0xf; if (v != numIndex[3]) drawOut(3, v);
		v = time.minute % 10; if (v != numIndex[4]) drawOut(4, v);
	}
	if (time.second != s)
	{
		time.second = s;
		intptr_t v = (intptr_t)(time.second / 10) & 0xf; if (v != numIndex[6]) drawOut(6, v);
		v = time.second % 10; if (v != numIndex[7]) drawOut(7, v);
	}
	if (isNA) { numIndex[2] = numIndex[5] = 10; drawOut(2, 10); drawOut(5, 10); isNA = false; }
	return displayChanged;
}

bool CMinTimeDisplay::setNA()
{
	if (isNA) return false;
	isNA = true; time.Set(-1, -1, -1); for (intptr_t i = 0; i < 8; i++) numIndex[i] = -1;
	CTimeDisplay::drawOut(numRectNA, hBitmapNA); return true;
}

void CMinTimeDisplay::drawOut(intptr_t id, intptr_t n)
{
	numIndex[id] = n;
	if (n >= 0 && n < 11)
	{
		CTimeDisplay::drawOut(numRect[id], nmTime[n].bmp); displayChanged = true;
	}
}
